const TokenKey = "sean_yan_cloud_template_token";
const RefreshKey = "sean_yan_cloud_refresh_token";
const LocationKey = "sean_yan_cloud_refresh_token";
const UserInfoKey = "sean_yan_cloud_user-info";

export function getToken() {
  return localStorage.getItem(TokenKey);
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token);
}

export function setRefreshToken(token) {
  return localStorage.setItem(RefreshKey, token);
}

export function setUserInfo(key, value) {
  if (UserInfoKey && value) {
    return localStorage.setItem(UserInfoKey, JSON.stringify(value));
  } else {
    return {};
  }
}

export function getUserInfo() {
  if (UserInfoKey) {
    let value = localStorage.getItem(UserInfoKey);
    return JSON.parse(value);
  } else {
    return {};
  }
}

export function getRefreshToken() {
  return localStorage.getItem(RefreshKey);
}

export function removeToken() {
  return localStorage.removeItem(TokenKey);
}

export function clearStorage() {
  localStorage.removeItem(TokenKey);
  localStorage.removeItem(LocationKey);
  localStorage.removeItem(RefreshKey);
}

export function getLocation() {
  let data = localStorage.getItem(LocationKey);
  if (data) {
    try {
      return JSON.parse(data);
    } catch (e) {
      return null;
    }
  }
  return null;
}

export function setLocation(data) {
  return localStorage.setItem(LocationKey, JSON.stringify(data));
}

export function removeLocation() {
  return localStorage.removeItem(LocationKey);
}
